package top.deepdesigner.controller.promotion;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.deepdesigner.annotation.RequiresPermissionsDesc;
import top.deepdesigner.pojo.DtsCoupon;
import top.deepdesigner.pojo.DtsCouponUser;
import top.deepdesigner.service.DtsCouponService;
import top.deepdesigner.service.DtsCouponUserService;
import top.deepdesigner.util.ResponseUtil;
import top.deepdesigner.vo.CommonListVO;

import java.util.HashMap;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/23 星期五 11:03:22 <br/>
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/coupon")
public class CouponController {
  @Autowired
  private DtsCouponService couponService;

  @Autowired
  private DtsCouponUserService couponUserService;

  /**
   * 优惠劵列表
   *
   * @param params 参数个数
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:coupon:list"})
  @GetMapping("/list")
  public Object list(@RequestParam HashMap<String, Object> params,
                     @RequestParam(defaultValue = "") String name,
                     @RequestParam(defaultValue = "") Integer type,
                     @RequestParam(defaultValue = "") Integer status) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");

    CommonListVO<DtsCoupon> couponListVO = new CommonListVO<>();
    //获取分页数量
    Long count = couponService.getCouponCount();
    List<DtsCoupon> couponList = couponService.getCouponList(page, limit, sort, order, name, type, status);
    couponListVO.setTotal(count);
    couponListVO.setItems(couponList);
    return ResponseUtil.ok(couponListVO);
  }

  /**
   * 此优惠劵基本信息
   *
   * @param id id
   * @return {@link Object}
   */
  @GetMapping("/read")
  public Object read(@RequestParam Integer id) {
    DtsCoupon coupon = couponService.getCouponById(id);
    return ResponseUtil.ok(coupon);
  }

  /**
   * 此优惠劵使用的用户列表
   *
   * @param params 参数个数
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "优惠券详情"}, button = "获取列表")
  @RequiresPermissions(value = {"admin:coupon:listuser"})
  @GetMapping("/listuser")
  public Object listuser(@RequestParam HashMap<String, Object> params,
                         @RequestParam(defaultValue = "") String name,
                         @RequestParam(defaultValue = "") Integer status,
                         @RequestParam(defaultValue = "") Integer couponId) {
    Integer page = Integer.parseInt((String) params.get("page"));
    Integer limit = Integer.parseInt((String) params.get("limit"));
    String sort = (String) params.get("sort");
    String order = (String) params.get("order");

    CommonListVO<DtsCouponUser> couponUserListVO = new CommonListVO<>();
    //获取分页数量
    Long count = couponUserService.getCouponUserCount();
    List<DtsCouponUser> couponUsers = couponUserService.getCouponUserList(page, limit, sort, order, name, couponId,status);
    couponUserListVO.setTotal(count);
    couponUserListVO.setItems(couponUsers);
    return ResponseUtil.ok(couponUserListVO);
  }

  /**
   * 创建优惠劵
   *
   * @param coupon 优惠券
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "添加")
  @RequiresPermissions(value = {"admin:coupon:create"})
  @PostMapping("/create")
  public Object create(@RequestBody DtsCoupon coupon) {
    System.out.println("coupon = " + coupon);
    DtsCoupon couponRes = couponService.createCoupon(coupon);
    if (couponRes != null) return ResponseUtil.ok(couponRes);
    return ResponseUtil.fail();
  }

  /**
   * 更新优惠劵
   *
   * @param coupon 优惠券
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "修改")
  @RequiresPermissions(value = {"admin:coupon:update"})
  @PostMapping("/update")
  public Object update(@RequestBody DtsCoupon coupon) {
    System.out.println("coupon = " + coupon);
    DtsCoupon couponRes = couponService.updateCoupon(coupon);
    if (couponRes != null) return ResponseUtil.ok(couponRes);
    return ResponseUtil.fail();
  }

  /**
   * 删除优惠劵
   *
   * @param coupon 优惠券
   * @return {@link Object}
   */
  @RequiresPermissionsDesc(menu = {"推广管理", "优惠券管理"}, button = "删除")
  @RequiresPermissions(value = {"admin:coupon:delete"})
  @PostMapping("/delete")
  public Object delete(@RequestBody DtsCoupon coupon) {
    couponService.deleteCoupon(coupon);
    return ResponseUtil.ok();
  }
}
